﻿using Equipment.Errorcode;
using Equipment.Errorcode.ErrorCode;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Equipment.API.Filter
{
    /// <summary>
    /// 全局异常过滤器
    /// </summary>
    public class GlobalExceptionFilter : IExceptionFilter
    {
        /// <summary>
        /// 日志
        /// </summary>
        private readonly ILogger<GlobalExceptionFilter> _logger;

        /// <summary>
        /// 注入
        /// </summary>
        /// <param name="logger"></param>
        public GlobalExceptionFilter(ILogger<GlobalExceptionFilter> logger)
        {
            _logger = logger;
        }
        public void OnException(ExceptionContext context)
        {
            _logger.LogError("全局异常过滤器发生异常" + context.Exception.Message);
            context.Result = new JsonResult(ApiResult.Fail(context.Exception.Message));
            context.ExceptionHandled = true;
        }
    }
}
